package com.sunyur.tailor.channel.web.outer.order.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.address.model.AddressListModel;
import com.sunyur.tailor.channel.service.inner.order.param.CreateOrderParam;
import com.sunyur.tailor.channel.service.inner.order.service.SunyurOrderService;
import com.sunyur.tailor.channel.web.outer.order.dto.CreateOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author xuhui
 * @Description 订单控制层
 * @Date 15:45 2021/6/1
 * @Param
 * @return
 **/
@Slf4j
@RestController
@RequestMapping("/outer/order")
public class OrderController {

    @Autowired
    private SunyurOrderService sunyurOrderService;

    /**
     * 批量创建库存台账
     */
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult create(@RequestBody @Valid CreateOrderDTO createOrderDTO) {
        log.info("OrderController create param:{}", JSON.toJSONString(createOrderDTO));
        ChannelResult<List<AddressListModel>> result = new ChannelResult<>();
        try {
            //数据转换
            CreateOrderParam createOrderParam = new CreateOrderParam();
            BeanUtils.copyProperties(createOrderDTO, createOrderParam);
            result = sunyurOrderService.create(createOrderParam);
        } catch (Exception e) {
            log.error("OrderController create param exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

}